package cn.itrip.common;





import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class DtoUtil {
    /**
     * 随机验证码
     * @return
     */
    public String achieveCode() {  //由于数字 1 、 0 和字母 O 、l 有时分不清楚，所以，没有数字 1 、 0
        String[] beforeShuffle= new String[] { "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
                "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a",
                "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
                "w", "x", "y", "z" };
        //将数组转换为集合
        List list = Arrays.asList(beforeShuffle);
        //打乱集合顺序
        Collections.shuffle(list);
        StringBuilder authCode = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            //将集合转化为字符串
            authCode.append(list.get(i));
        }
        //截取字符串第4到8
        return authCode.toString().substring(3, 8);
    }

    //发送邮件代码
    public static void sendAuthCodeEmail(String authCode,String userCode) {
        try {
            SimpleEmail mail = new SimpleEmail();
            //发送邮件的服务器
            mail.setHostName("smtp.qq.com");
            //刚刚记录的授权码，是开启SMTP的密码
            mail.setAuthentication("443945416@qq.com","xdblhisdttfbbihj");
            //发送邮件的邮箱和发件人
            mail.setFrom("443945416@qq.com","滕召伟");
            //使用安全链接
            mail.setSSLOnConnect(true);
            //接收的邮箱
            mail.addTo(userCode);
            //设置邮件的主题
            mail.setSubject("注册验证码");
            //设置邮件的内容
            mail.setMsg("尊敬的用户:你好!\n 你的注册验证码为:" + authCode+"\n"+"(有效期为一分钟)");
            mail.send();//发送
        } catch (EmailException e) {
            e.printStackTrace();

        }
    }
}
